const gameVue =  new Vue({
    el: '#gamePage',
    data: {
      name:"",
      subForm : {
        userId:"",
        poker:""
      },
      socket:null,
      message:"",
      gameInfo :{},
      prods :[],
    },
    mounted (){
      // this.findGameInfo()
      this.socket = io("39.96.186.114:32514"); //通过ip和端口建立一个socket client
      this.socket.on('connect', function(){
        console.log('connect a socket client');
    });
      setInterval(() => {
        this.findGameInfo()
      },3000)
    },
    methods :{
        findGameInfo : async function(){
          if(!this.subForm.userId){
            this.message ="请收入用户ID"
            return;
          }
          this.gameInfo = await this._post("/game/info", {
            ...this.subForm
          })
          for(let gameSeat of this.gameInfo.gameSeats){
            gameSeat.holds= gameSeat.holds.sort((a, b)=> a-b);
          }
          console.log(this.gameInfo)
          for(let gameSeat of this.gameInfo.gameSeats){
            gameSeat.images = [];
            for(let poker of gameSeat.holds){
              gameSeat.images.push("/image/"+poker+".png")
            }
          }
        },
        query : function(){
          this._post("/set1234", {
            ...this.subForm
          })
        },
        sitdown : function(){
          this.socket.emit("open",{
            userId:+this.subForm.userId
          })
          let self = this;
          this.socket.on("open_result", function(data){
            console.log(data)
            let {roomId} = data;
            if(!roomId) return;
            if(!self.subForm.poker || self.subForm.poker>3){
              return;
            }
            self.socket.emit("player_sit", {
              roomId,
              clientIp:"110.179.201.127",
              index:+self.subForm.poker,
              lat:37.56,
              lon:112.13
            })
            self.socket.on("player_sit_push", function(data){
              console.log("111111111111111")
              console.log(data)
            })
            self.socket.on("new_player_sitdown_push", function(data){
              console.log("2222222")
              console.log(data)
            })
          })
          
        },
        _post :function(url, data = {},orgin, type = 'application/json') {
          let self = this;
          return new Promise((resolve, reject) => {
            axios.post(url, data, {
              headers: {
                'Content-Type': type
              }
            }).then(response => {
              if(response.data.code!=0 && response.data.message){
                self.message = response.data.message
                return;
              }
              if(orgin){
                resolve(response.data)
              }else{
                resolve(response.data.data)
              }
            }, err => {
              console.log(err)
              reject(err)
            })
          })
        }
    }
})

